package com.hbsx.why.ui;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hbsx.why.R;
import com.hbsx.why.adapter.ListAdapter;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.utils.refresh.PullListView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by liub on 2017/6/27 18:30.
 */

public class DemoRefreshListView extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.demo_listview)
    PullListView demoListview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout mPullToRefreshLayout;
    @BindView(R.id.btn_top_right)
    ImageView btnTopRight;
    @BindView(R.id.text_top_right)
    TextView textTopRight;
    private ListAdapter listAdapter;
    private Context context = this;
    private List<String> stringList;
    private int page =0;

    @Override
    protected int getLayoutResId() {
        return R.layout.demo_listview_refresh;
    }

    @Override
    public void initView() {
        tvTopTitle.setText("listview 刷新加载更多");
        textTopRight.setText("自动加载");
        textTopRight.setVisibility(View.VISIBLE);
        demoListview.setPullDownEnable(true);
        demoListview.setPullUpEnable(true);
        listAdapter = new ListAdapter(context, stringList);
        demoListview.setAdapter(listAdapter);
        page=1;
        getData(page);
        demoListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        mPullToRefreshLayout.setOnRefreshListener(this);
    }

    private void getData(int page) {
        if (page == 1) {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                stringList.add("数据刷新--" + i);
            }
            listAdapter.updateListView(stringList);
        } else {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                stringList.add("加载数据--" + page + i);
            }
            listAdapter.loadMoreListView(stringList);
        }
        listAdapter.notifyDataSetChanged();
        //mPullToRefreshLayout.autoLoadMove(true);
       // mPullToRefreshLayout.getRefreshFooterView().setVisibility(View.GONE);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        pullToRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Toast.makeText(getApplicationContext(), "onRefresh1200", Toast.LENGTH_LONG).show();
                mPullToRefreshLayout.refreshFinish(true);
                page = 1;
                getData(page);
            }
        }, 1200); // 2秒后刷新
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        pullToRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPullToRefreshLayout.loadMoreFinish(true);
                page = page + 1;
                getData(page);
            }
        }, 500); // 2秒后刷新


    }

    @OnClick({R.id.btn_top_right, R.id.text_top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_top_right:
                break;
            case R.id.text_top_right:
//                mPullToRefreshLayout.autoLoadMove(true);
                // mPullToRefreshLayout.autoRefresh();
                break;
        }
    }
}
